﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.UserInterface
{
    public partial class cbStreet : UserControl
    {
        public string Street_ID;
        public string Street_Name;
        //──────────────────────────────────────────────────────────────────
        public cbStreet()
        {
            InitializeComponent();
            this.Load += new EventHandler(cbStreet_Load);
            cbStrtName.SelectedValueChanged += new EventHandler(cbStrtName_SelectedIndexChanged);
            this.cbStrtName.TextChanged += new EventHandler(cbStrtName_TextChanged);
        }

        void cbStrtName_TextChanged(object sender, EventArgs e)
        {
            if (this.cbStrtName.Text.Trim() == "")
                this.cbStrtName_SelectedIndexChanged(sender, e);
        }
        //──────────────────────────────────────────────────────────────────
        void  cbStreet_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    if (this.cbStrtName.DataSource == null)
            //    {
            //        DataTable dt = new CMS_GYM.Address().dt_getStreet();
            //        if (dt == null) return;
            //        this.cbStrtName.DataSource = new DataView(dt);
            //        cbStrtName.DisplayMember = dt.Columns[1].ColumnName;
            //        cbStrtName.ValueMember = dt.Columns[0].ColumnName;
            //        cbStrtName_SelectedIndexChanged(sender, e);
            //    }
            //}
            //catch (Exception) { }
        }
        public void LoadData()
        {
            try
            {
                if (this.cbStrtName.DataSource == null)
                {
                    DataTable dt = new CMS_GYM.Address().dt_getStreet();
                    if (dt == null) return;
                    this.cbStrtName.DataSource = new DataView(dt);
                    cbStrtName.DisplayMember = dt.Columns[1].ColumnName;
                    cbStrtName.ValueMember = dt.Columns[0].ColumnName;
                    cbStrtName_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception) { }
        }
        //──────────────────────────────────────────────────────────────────
        void cbStrtName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged();
        }
        //──────────────────────────────────────────────────────────────────
        private void SelectedIndexChanged()
        {
            if (cbStrtName.SelectedValue == null)
            {
                Street_ID = "-1";
                Street_Name = "";
                return;
            }
            //Street_ID = cbStrtName.getSelectedValue();
            Street_ID = cbStrtName.SelectedValue == null ? string.Empty : cbStrtName.SelectedValue.ToString();
            Street_Name = cbStrtName.Text;
        }
        //──────────────────────────────────────────────────────────────────
        public void SetStreet(string s_StreetID)
        {
            //cbStrtName.SetSelectedValue(s_StreetID);
            if (s_StreetID == "" || s_StreetID == null || s_StreetID == "NULL")
            {
                cbStrtName.SelectedValue = DBNull.Value;
                cbStrtName.Text = "";
            }
            else
            {
                cbStrtName.SelectedValue = s_StreetID;

            }

        }
        //──────────────────────────────────────────────────────────────────
        public void SetDataSource(DataTable dtStreet)
        {
            this.cbStrtName.DataSource = dtStreet;
            cbStrtName.DisplayMember = dtStreet.Columns[1].ColumnName;
            cbStrtName.ValueMember = dtStreet.Columns[0].ColumnName;            
            SelectedIndexChanged();   
        }
        //────────────────────────────────────────────────────────────────── 
    }
}
